/*
   Copyright (c) 2023 李伟国
   shiro-learner is licensed under Mulan PSL v2.
   You can use this software according to the terms and conditions of the Mulan PSL v2. 
   You may obtain a copy of Mulan PSL v2 at:
            http://license.coscl.org.cn/MulanPSL2 
   THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.  
   See the Mulan PSL v2 for more details. 
*/

package cn.learner.service.impl;

import cn.learner.config.page.PageFactory;
import cn.learner.constant.enumeration.StatusConstantEnum;
import cn.learner.entity.Role;
import cn.learner.entity.User;
import cn.learner.entity.UserRole;
import cn.learner.mapper.RoleMapper;
import cn.learner.mapper.UserMapper;
import cn.learner.mapper.UserRoleMapper;
import cn.learner.service.IRoleService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public List<Role> findByUsername(String username) {
        User user = userMapper.selectByUsername(username);
        return getRolesByUser(user);
    }

    @Override
    public List<Role> findByEmail(String email) {
        User user = userMapper.selectByEmail(email);
        return getRolesByUser(user);
    }

    @Override
    public Page<Role> findAll() {
        return this.baseMapper.selectAll(PageFactory.defaultPage());
    }

    @Override
    public Boolean updateRole(Role role) {
        Role roleOld = this.baseMapper.selectById(role.getId());
        if(roleOld == null||roleOld.equals(role)){
            return false;
        }
        return this.baseMapper.updateById(role) > 0;
    }

    @Override
    public Boolean deleteRole(Role role) {
        if(role == null){
            return false;
        }
        Role roleOld = this.baseMapper.selectById(role.getId());
        if(!role.equals(roleOld)){
            return false;
        }
        return   1 < this.baseMapper.updateStatusRole(role.getId(), StatusConstantEnum.DELETED.getCode());
    }

    @Override
    public Boolean resetRole(Role role) {
        if(role == null){
            return false;
        }
        Role roleOld = this.baseMapper.selectById(role.getId());
        if(!role.equals(roleOld)) {
            return false;
        }
        return this.baseMapper.updateStatusRole(role.getId(),StatusConstantEnum.ENABLE.getCode()) > 0;
    }

    private List<Role> getRolesByUser(User user) {
        List<UserRole> userRoles = userRoleMapper.selectByUserId(user.getId());
        List<Role> roles = new ArrayList<>();
        for(UserRole userRole : userRoles){
            Role role = this.baseMapper.selectById(userRole.getRoleId());
            roles.add(role);
        }
        return roles;
    }
    @Override
    public List<Role> findRoleAllOptions(){
        return this.baseMapper.selectAllOptions();
    }

    @Override
    public List<Role> findByUserId(Long userId) {
        return this.baseMapper.selectByUserId(userId);
    }
}
